import { observable, action } from 'mobx-miniprogram'

export const store = observable({
  /**
   * 数据字段
   */
  token: wx.getStorageSync('token') || "",
  FIL: wx.getStorageSync('FIL') || {},
  USDT: wx.getStorageSync('USDT') || {},
  productList: wx.getStorageSync('productList') || [],

  /**
   * 修改token的值
   */
  setToken: action(function (token) {
    this.token = token
    wx.setStorageSync('token', token)
  }),
  /**
   * 获取钱包信息
   */
  getWallet: action(function (name) {
    wx.showLoading({ title: '加载中…', mask: true })
    wx.cloud.callFunction({
      name: 'wallet',
      data: {
        name
      },
      complete: res => {
        wx.hideLoading()
        const r = res.result || {}
        this[name] = r.result || {}
        wx.setStorageSync(name, r.result || {})
      }
    })
  }),
  /**
   * 获取产品列表
   */
  getProduct: action(function () {
    wx.showLoading({ title: '加载中…', mask: true })
    wx.cloud.callFunction({
      name: 'GetProduct',
      complete: res => {
        wx.hideLoading()
        const r = res.result || {}
        this.productList = r.result || []
        wx.setStorageSync('productList', r.result || [])
      }
    })
  })
})